package com.lt.test.consumer.web.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.lt.test.api.KtProdService;
import com.lt.test.common.constant.HttpRequestConstant;
import com.lt.test.common.exception.CustomException;
import com.lt.test.common.util.WebUtils;
import com.lt.test.entity.DTO.ActiveUser;
import com.lt.test.entity.DTO.PageDTO;
import com.lt.test.entity.DTO.ResponseDTO;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;


@Controller
@RequestMapping("/ktProd")
public class KtProdController {
	 private static final Logger logger = LoggerFactory.getLogger(KtProdController.class);
	    @Reference(version = "1.0.0")
	    private KtProdService ktProdService;


	    /**
	     * 商品价格类型信息
	     *
	     * @param request
	     * @param response
	     * @return
	     */
	    @RequestMapping("/getProdPriceType")
	    public
	    @ResponseBody
	    List<Map<String, Object>> getProdPriceType(HttpServletRequest request, HttpServletResponse response) {
	    	List<Map<String, Object>> result = ktProdService.getProdPriceType();
	        return result;
	    }


	    /**
	     * 商品子类型信息
	     *
	     * @param request
	     * @param response
	     * @return
	     */
	    @RequestMapping("/getSubProdType")
	    public
	    @ResponseBody
	    List<Map<String, Object>> getSubProdType(HttpServletRequest request, HttpServletResponse response) {
	    	Map<String,String> parameter = WebUtils.getAllParamMap(request);
	    	List<Map<String, Object>> result = ktProdService.getSubProdType(parameter);
	        return result;
	    }

	    /**
	     * 字典
	     *
	     * @param request
	     * @param response
	     * @return
	     */
	    @RequestMapping("/getDict")
	    public
	    @ResponseBody
	    List<Map<String, Object>> getDict(HttpServletRequest request, HttpServletResponse response) {
	    	Map<String,String> parameter = WebUtils.getAllParamMap(request);
	    	List<Map<String, Object>> result = ktProdService.getDict(parameter);
	        return result;
	    }


	    /**
	     * 某商品类型的销售属性
	     *
	     * @param request
	     * @param response
	     * @return
	     */
	    @RequestMapping("/getProdProperty")
	    public
	    @ResponseBody
	    List<Map<String, Object>> getProdProperty(HttpServletRequest request, HttpServletResponse response) {
	    	Map<String,String> parameter = WebUtils.getAllParamMap(request);
	    	List<Map<String, Object>> result = ktProdService.getProdProperty(parameter);
	        return result;
	    }

	    /**
	     * 分页商品类型信息
	     *
	     * @param request
	     * @param response
	     * @return
	     */
	    @RequestMapping("/getProdType")
	    public
	    @ResponseBody
	    PageDTO<Map<String, Object>> getProductType(HttpServletRequest request, HttpServletResponse response) {
	    	Integer page = Integer.valueOf(request.getParameter("page"));
			Integer pageSize = Integer.valueOf(request.getParameter("rows"));
	        String filters = request.getParameter("filters");
	        PageDTO<Map<String, Object>> result = ktProdService.getProdTypeListByPage(page, pageSize, filters);
	        return result;
	    }

	    /**
	     * 分页商品品牌信息
	     *
	     * @param request
	     * @param response
	     * @return
	     */
	    @RequestMapping("/getProdBrandListByPage")
	    public
	    @ResponseBody
	    PageDTO<Map<String, Object>> getProdBrandListByPage(HttpServletRequest request, HttpServletResponse response) {
	    	Integer page = Integer.valueOf(request.getParameter("page"));
			Integer pageSize = Integer.valueOf(request.getParameter("rows"));
	        String filters = request.getParameter("filters");
	        PageDTO<Map<String, Object>> result = ktProdService.getProdBrandListByPage(page, pageSize, filters);
	        return result;
	    }

	    /**
	     * 分页商品信息
	     *
	     * @param request
	     * @param response
	     * @return
	     */
	    @RequestMapping("/getProdInfo")
	    public
	    @ResponseBody
	    PageDTO<Map<String, Object>> getProductInfo(HttpServletRequest request, HttpServletResponse response) {
	    	Integer page = Integer.valueOf(request.getParameter("page"));
			Integer pageSize = Integer.valueOf(request.getParameter("rows"));
	        String filters = request.getParameter("filters");
	        PageDTO<Map<String, Object>> result = ktProdService.getProdInfoListByPage(page, pageSize, filters);
	        return result;
	    }

	    /**
	     * 获取组合商品条目
	     *
	     * @param request
	     * @param response
	     * @return
	     */
	    @RequestMapping("/getGroupProductItem")
	    public
	    @ResponseBody
	    ResponseDTO getGroupProductItem(HttpServletRequest request, HttpServletResponse response) {
	    	Map<String,String> parameter = WebUtils.getAllParamMap(request);
	        List<Map<String, Object>> result = ktProdService.getGroupProductItem(parameter);
	        ResponseDTO responseDTO = new ResponseDTO();
	        responseDTO.setCode("200");
	        responseDTO.setSuccess(true);
	        responseDTO.setData(result);
	        responseDTO.setMessage("操作成功");
	    	return responseDTO;
	    }


	    /**
	     * 分页店铺缺货商品信息
	     *
	     * @param request
	     * @param response
	     * @return
	     */
	    @RequestMapping("/getRecommendProdByPage")
	    public
	    @ResponseBody
	    PageDTO<Map<String, Object>> getRecommendProdByPage(HttpServletRequest request, HttpServletResponse response) {
	    	Integer page = Integer.valueOf(request.getParameter("page"));
			Integer pageSize = Integer.valueOf(request.getParameter("rows"));
	        String filters = request.getParameter("filters");
	        PageDTO<Map<String, Object>> result = ktProdService.getRecommendProdByPage(page, pageSize, filters);
	        return result;
	    }

	    /**
	     * 总部快速采购商品信息
	     *
	     * @param request
	     * @param response
	     * @return
	     */
	    @RequestMapping("/getCenterRecommendProdByPage")
	    public
	    @ResponseBody
	    PageDTO<Map<String, Object>> getCenterRecommendProdByPage(HttpServletRequest request, HttpServletResponse response) {
	    	Integer page = Integer.valueOf(request.getParameter("page"));
			Integer pageSize = Integer.valueOf(request.getParameter("rows"));
	        String filters = request.getParameter("filters");
	        PageDTO<Map<String, Object>> result = ktProdService.getCenterRecommendProdByPage(page, pageSize, filters);
	        return result;
	    }


	    /**
	     * 分页商品价格类型信息
	     *
	     * @param request
	     * @param response
	     * @return
	     */
	    @RequestMapping("/getProdTypePriceListByPage")
	    public
	    @ResponseBody
	    PageDTO<Map<String, Object>> getProdTypePriceListByPage(HttpServletRequest request, HttpServletResponse response) {
	    	Integer page = Integer.valueOf(request.getParameter("page"));
			Integer pageSize = Integer.valueOf(request.getParameter("rows"));
	        String filters = request.getParameter("filters");
	        PageDTO<Map<String, Object>> result = ktProdService.getProdTypePriceListByPage(page, pageSize, filters);
	        return result;
	    }



	    /**
	     * 分页商品价格信息
	     *
	     * @param request
	     * @param response
	     * @return
	     */
	    @RequestMapping("/getProdPrice")
	    public
	    @ResponseBody
	    PageDTO<Map<String, Object>> getProductPrice(HttpServletRequest request, HttpServletResponse response) {
	    	Integer page = Integer.valueOf(request.getParameter("page"));
			Integer pageSize = Integer.valueOf(request.getParameter("rows"));
	        String filters = request.getParameter("filters");
	        PageDTO<Map<String, Object>> result = ktProdService.getProdPriceListByPage(page, pageSize, filters);
	        return result;
	    }

	    /**
	     * 分页店铺信息
	     *
	     * @param request
	     * @param response
	     * @return
	     */
	    @RequestMapping("/getShopListByPage")
	    public
	    @ResponseBody
	    PageDTO<Map<String, Object>> getShopListByPage(HttpServletRequest request, HttpServletResponse response) {
	    	Integer page = Integer.valueOf(request.getParameter("page"));
			Integer pageSize = Integer.valueOf(request.getParameter("rows"));
	        String filters = request.getParameter("filters");
	        PageDTO<Map<String, Object>> result = ktProdService.getShopListByPage(page, pageSize, filters);
	        return result;
	    }

	    /**
	     * 分页店铺信息
	     *
	     * @param request
	     * @param response
	     * @return
	     */
	    @RequestMapping("/getSupplierProdList")
	    public
	    @ResponseBody
	    PageDTO<Map<String, Object>> getSupplierProdList(HttpServletRequest request, HttpServletResponse response) {
	    	Integer page = Integer.valueOf(request.getParameter("page"));
			Integer pageSize = Integer.valueOf(request.getParameter("rows"));
	        String filters = request.getParameter("filters");
	        PageDTO<Map<String, Object>> result = ktProdService.getSupplierProdList(page, pageSize, filters);
	        return result;
	    }


	    /**
	     * 保存供应商和商品关系
	     *
	     * @param request
	     * @param response
	     * @return json
	     */
	    @RequestMapping("/saveSupplierProd")
	    public
	    @ResponseBody
	    ResponseDTO saveSupplierProd(HttpServletRequest request, HttpServletResponse response) {
	        ResponseDTO responseDTO = new ResponseDTO();
	        Map<String,String> parameter = WebUtils.getAllParamMap(request);
	        try {
	            ktProdService.saveSupplierProd(parameter);
	            responseDTO.setCode("200");
	            responseDTO.setSuccess(true);
	            responseDTO.setMessage("操作成功");
	        } catch (CustomException e) {
				responseDTO.setCode(HttpRequestConstant.SERVER_ERROR_CODE);
				responseDTO.setMessage(e.getMessage());
				e.printStackTrace();
			}catch (Exception e) {
	            e.printStackTrace();
	            responseDTO.setCode("500");
	            responseDTO.setMessage("服务器异常");
	        }
	        return responseDTO;

	    }


	    /**
	     * 保存商品类别
	     *
	     * @param request
	     * @param response
	     * @return json
	     */
	    @RequestMapping("/saveProdType")
	    public
	    @ResponseBody
	    ResponseDTO saveProdType(HttpServletRequest request, HttpServletResponse response) {
	        ResponseDTO responseDTO = new ResponseDTO();
	        Map<String,String> parameter = WebUtils.getAllParamMap(request);
	        try {
	        	Subject subject = SecurityUtils.getSubject();
	            ActiveUser activeUser = (ActiveUser) subject.getPrincipal();
	            parameter.put("operator", activeUser.getUserId());
	            ktProdService.saveProdType(parameter);
	            responseDTO.setCode("200");
	            responseDTO.setSuccess(true);
	            responseDTO.setMessage("操作成功");
	        } catch (Exception e) {
	            e.printStackTrace();
	            responseDTO.setCode("500");
	            responseDTO.setMessage("服务器异常");
	        }
	        return responseDTO;

	    }

	    /**
	     * 保存商品品牌
	     *
	     * @param request
	     * @param response
	     * @return json
	     */
	    @RequestMapping("/saveProdBrand")
	    public
	    @ResponseBody
	    ResponseDTO saveProdBrand(HttpServletRequest request, HttpServletResponse response) {
	        ResponseDTO responseDTO = new ResponseDTO();
	        Map<String,String> parameter = WebUtils.getAllParamMap(request);
	        try {
	            ktProdService.saveProdBrand(parameter);
	            responseDTO.setCode("200");
	            responseDTO.setSuccess(true);
	            responseDTO.setMessage("操作成功");
	        } catch (Exception e) {
	            e.printStackTrace();
	            responseDTO.setCode("500");
	            responseDTO.setMessage("服务器异常");
	        }
	        return responseDTO;

	    }

	    /**
	     * 保存商品
	     *
	     * @param request
	     * @param response
	     * @return json
	     */
	    @RequestMapping("/saveProd")
	    public
	    @ResponseBody
	    ResponseDTO saveProd(HttpServletRequest request, HttpServletResponse response) {
	        ResponseDTO responseDTO = new ResponseDTO();
	        Map<String,String> parameter = WebUtils.getAllParamMap(request);
	        try {
	        	Subject subject = SecurityUtils.getSubject();
	            ActiveUser activeUser = (ActiveUser) subject.getPrincipal();
	            parameter.put("operator", activeUser.getUserId());
	            ktProdService.saveProd(parameter);
	            responseDTO.setCode("200");
	            responseDTO.setSuccess(true);
	            responseDTO.setMessage("操作成功");
	        } catch (CustomException e) {
				responseDTO.setCode(HttpRequestConstant.SERVER_ERROR_CODE);
				responseDTO.setMessage(e.getMessage());
				e.printStackTrace();
			}catch (Exception e) {
	            e.printStackTrace();
	            responseDTO.setCode("500");
	            responseDTO.setMessage("服务器异常");
	        }
	        return responseDTO;

	    }

	    /**
	     * 价格下放
	     *
	     * @param request
	     * @param response
	     * @return json
	     */
	    @RequestMapping("/saveProdPrice2Shop")
	    public
	    @ResponseBody
	    ResponseDTO saveProdPrice2Shop(HttpServletRequest request, HttpServletResponse response) {
	        ResponseDTO responseDTO = new ResponseDTO();
	        Map<String,String> parameter = WebUtils.getAllParamMap(request);
	        try {
	        	Subject subject = SecurityUtils.getSubject();
	            ActiveUser activeUser = (ActiveUser) subject.getPrincipal();
	            parameter.put("operator", activeUser.getUserId());
	            ktProdService.saveProdPrice2Shop(parameter);
	            responseDTO.setCode("200");
	            responseDTO.setSuccess(true);
	            responseDTO.setMessage("操作成功");
	        } catch (Exception e) {
	            e.printStackTrace();
	            responseDTO.setCode("500");
	            responseDTO.setMessage("服务器异常");
	        }
	        return responseDTO;

	    }

	    /**
	     * 根据折扣批量设置商品价格
	     *
	     * @param request
	     * @param response
	     * @return json
	     */
	    @RequestMapping("/updateProdDiscountPriceByType")
	    public
	    @ResponseBody
	    ResponseDTO updateProdDiscountPriceByType(HttpServletRequest request, HttpServletResponse response) {
	        ResponseDTO responseDTO = new ResponseDTO();
	        Map<String,String> parameter = WebUtils.getAllParamMap(request);
	        try {
	            ktProdService.updateProdDiscountPriceByType(parameter);
	            responseDTO.setCode("200");
	            responseDTO.setSuccess(true);
	            responseDTO.setMessage("操作成功");
	        } catch (Exception e) {
	            e.printStackTrace();
	            responseDTO.setCode("500");
	            responseDTO.setMessage("服务器异常");
	        }
	        return responseDTO;

	    }


	    /**
	     * 根据商品获取供应商
	     *
	     * @param request
	     * @param response
	     * @return
	     */
	    @RequestMapping("/getProdSupplier")
	    public
	    @ResponseBody
	    List<Map<String, Object>> getProdSupplier(HttpServletRequest request, HttpServletResponse response) {
	    	 Map<String,String> parameter = WebUtils.getAllParamMap(request);
	    	List<Map<String, Object>> result = ktProdService.getProdSupplier(parameter);
	        return result;
	    }

	    /**
	     * 总部快速采购商品信息
	     *
	     * @param request
	     * @param response
	     * @return
	     */
	    @RequestMapping("/getCenterRecommendTightTobaccoByPage")
	    public
	    @ResponseBody
	    PageDTO<Map<String, Object>> getCenterRecommendTightTobaccoByPage(HttpServletRequest request, HttpServletResponse response) {
	    	Integer page = Integer.valueOf(request.getParameter("page"));
			Integer pageSize = Integer.valueOf(request.getParameter("rows"));
	        String filters = request.getParameter("filters");
	        PageDTO<Map<String, Object>> result = ktProdService.getCenterRecommendTightTobaccoByPage(page, pageSize, filters);
	        return result;
	    }


	    /**
	     * 删除紧俏烟
	     *
	     * @param request
	     * @param response
	     * @return json
	     */
	    @RequestMapping("/deleteTightTobacco")
	    public
	    @ResponseBody
	    ResponseDTO deleteTightTobacco(HttpServletRequest request, HttpServletResponse response) {
	        ResponseDTO responseDTO = new ResponseDTO();
	        try {
	            ktProdService.deleteTightTobacco();
	            responseDTO.setCode("200");
	            responseDTO.setSuccess(true);
	            responseDTO.setMessage("操作成功");
	        } catch (Exception e) {
	            e.printStackTrace();
	            responseDTO.setCode("500");
	            responseDTO.setMessage("服务器异常");
	        }
	        return responseDTO;

	    }

	    /**
	     * 保存紧俏烟
	     *
	     * @param request
	     * @param response
	     * @return json
	     */
	    @RequestMapping("/saveTightTobacco")
	    public
	    @ResponseBody
	    ResponseDTO saveTightTobacco(HttpServletRequest request, HttpServletResponse response) {
			ResponseDTO responseDTO = new ResponseDTO();
			Map<String, String> parameter = WebUtils.getAllParamMap(request);
			try {
				ktProdService.saveTightTobacco(parameter);
				responseDTO.setCode("200");
				responseDTO.setSuccess(true);
				responseDTO.setMessage("操作成功");
			} catch (Exception e) {
				e.printStackTrace();
				responseDTO.setCode("500");
				responseDTO.setMessage("服务器异常");
			}
			return responseDTO;

		}

	/**
	 * 获取库存
	 *
	 * @param request
	 * @param response
	 * @return json
	 */
	@RequestMapping("/gatStockProd")
		public
		@ResponseBody
		ResponseDTO gatStockProd(HttpServletRequest request, HttpServletResponse response) {
			ResponseDTO responseDTO = new ResponseDTO();
			Map<String, String> parameter = WebUtils.getAllParamMap(request);
			try {
				Map<String, Object> data = ktProdService.gatStockProd(parameter);
				responseDTO.setCode("200");
				responseDTO.setSuccess(true);
				responseDTO.setData(data);
				responseDTO.setMessage("操作成功");
			} catch (Exception e) {
				e.printStackTrace();
				responseDTO.setCode("500");
				responseDTO.setMessage("服务器异常");
			}
			return responseDTO;
		}

}
